package com.ruoyi.project.company.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.company.domain.Company;
import com.ruoyi.project.company.mapper.CompanyMapper;
import com.ruoyi.project.company.service.ICompanyService;
/**
 * 
 * @作者 290903152@qq.com
 * @时间 202003
 * @功能 毕业设计
 *
 */
@Service
public class CompanyServiceImpl implements ICompanyService {
	private XSSFSheet sheet;

	@Autowired
	private CompanyMapper companyMapper;
	
	@Override
	public Company selectCompanyById(String id) {
		return companyMapper.selectCompanyById(id);
	}

	@Override
	public List<Company> selectCompanyList(Company cp) {
		return companyMapper.selectCompanyList(cp);
	}

	@Override
	public int insertCompany(Company cp) {
		return companyMapper.insertCompany(cp);
	}

	@Override
	public int updateCompany(Company cp) {
		return companyMapper.updateCompany(cp);
	}

	@Override
	public int deleteCompany(String ids) {
		return companyMapper.deleteCompany(Convert.toStrArray(ids));
	}
	
	@Override
	public List<Company> selectCompanyListBythr(Company cp) {
		return companyMapper.selectCompanyListBythr(cp);
	}
	
	public void ExcelData(MultipartFile multipartFile) {
        try {
            //String encoding = "GBK";
            File excel = new File(multipartFile.getOriginalFilename());

                String[] split = excel.getName().split("\\.");  //.是特殊字符，需要转义！！！！！
                Workbook wb;
                //根据文件后缀（xls/xlsx）进行判断
                if ( "xls".equals(split[1])){
//                    FileInputStream fis = new FileInputStream(excel);   //文件流对象
                    wb = new HSSFWorkbook(new POIFSFileSystem(multipartFile.getInputStream()));
                }else if ("xlsx".equals(split[1])){
                    wb = new XSSFWorkbook(multipartFile.getInputStream());
                }else {
                    System.out.println("文件类型错误!");
                    return;
                }

                //开始解析
                Sheet sheet = wb.getSheetAt(0);     //读取sheet 0

                int firstRowIndex = sheet.getFirstRowNum()+1;   //第一行是列名，所以不读
                int lastRowIndex = sheet.getLastRowNum();
                System.out.println("firstRowIndex: "+firstRowIndex);
                System.out.println("lastRowIndex: "+lastRowIndex);

                for(int rIndex = firstRowIndex; rIndex <= lastRowIndex; rIndex++) {   //遍历行
                    System.out.println("rIndex: " + rIndex);
                    Row row = sheet.getRow(rIndex);
                    if (row != null) {
                        int firstCellIndex = row.getFirstCellNum();
                        int lastCellIndex = row.getLastCellNum();
                        Company cp = new Company();
                        for (int cIndex = firstCellIndex; cIndex < lastCellIndex; cIndex++) {   //遍历列
                            Cell cell = row.getCell(cIndex);
                            if (cell != null) {
                                System.out.println(trims(cell.toString()));
                                String cs = trims(cell.toString());
                                if(cs.length()==0) continue;
                                switch (cIndex) {
								case 0:cp.setFirmenu(cs);
									   break;
								case 1:cp.setFirrelation(cs);;
								   break;
								case 2:cp.setSecmenu(cs);
									   break;
								case 3:cp.setSecrelation(cs);;
								   break;
								case 4:cp.setThrmenu(cs);
									   break;

								}
                            }
                        }
                        companyMapper.insertCompany(cp);
                    }
                }
        } catch (Exception e) {
            e.printStackTrace();
        }


	}
	
	public static String trims(String str){
		if(str!=null){
			String regStartSpace = "^[　 ]*";
			String regEndSpace = "[　 ]*$";
			str = str.replaceAll(regStartSpace, "").replaceAll(regEndSpace, "");
			str = str.replaceAll("\\s*", "");
			str = str.replaceAll("　", "");
			return str;
		}else{
			return "";
		}
	}




}
